package com.leaderment.timatt.dataSource;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
public class DataSourceConfig {
	private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
	
	@Value("${spring.datasource.timatt.driver-class-name}")
	private String driverClassName;
	
	@Value("${spring.datasource.programManage.url}")
    private String dbUrl1;
 
    @Value("${spring.datasource.programManage.username}")
    private String username1;
 
    @Value("${spring.datasource.programManage.password}")
    private String password1;
 
    @Value("${spring.datasource.timatt.username}")
    private String username2;
 
    @Value("${spring.datasource.timatt.password}")
    private String password2;
 
    @Value("${spring.datasource.timatt.url}")
    private String dbUrl2;
 
    @Value("5")
    private int initialSize;
 
    @Value("5")
    private int minIdle;
 
    @Value("20")
    private int maxActive;
 
    @Value("60000")
    private int maxWait;
 
    /**
     * 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
     */
    @Value("60000")
    private int timeBetweenEvictionRunsMillis;
    /**
     * 配置一个连接在池中最小生存的时间，单位是毫秒
     */
    @Value("300000")
    private int minEvictableIdleTimeMillis;
 
    @Value("SELECT 1 FROM DUAL")
    private String validationQuery;
 
    @Value("true")
    private boolean testWhileIdle;
 
    @Value("false")
    private boolean testOnBorrow;
 
    @Value("false")
    private boolean testOnReturn;
    
    /**
     * 打开PSCache，并且指定每个连接上PSCache的大小
     */
    @Value("true")
    private boolean poolPreparedStatements;
 
    @Value("20")
    private int maxPoolPreparedStatementPerConnectionSize;
    /**
     * 配置监控统计拦截的filters，去掉后监控界面sql无法统计，'wall'用于防火墙
     */
    @Value("stat,wall,log4j")
    private String filters;
    /**
     * 通过connectProperties属性来打开mergeSql功能；慢SQL记录
     */
    @Value("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500")
    private String connectionProperties;

	@Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
	@Primary
    public DataSource dataSource() throws PropertyVetoException {
//        return getDruidDataSource(username1, password1, dbUrl1);
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
//        driverClassNmae
        dataSource.setDriverClass(driverClassName);
        dataSource.setJdbcUrl(dbUrl1);
        dataSource.setUser(username1);
        dataSource.setPassword(password1);
        dataSource.setMaxPoolSize(20);
        dataSource.setMinPoolSize(5);
        dataSource.setInitialPoolSize(10);
        dataSource.setMaxIdleTime(300);
        dataSource.setAcquireIncrement(5);
        dataSource.setIdleConnectionTestPeriod(60);
        
        return dataSource;
    }
 
    @Bean(name = "secondDataSource")
    @Qualifier("secondDataSource")
    public DataSource commonDataSource() throws PropertyVetoException {
//        return getDruidDataSource(username2, password2, dbUrl2);
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driverClassName);
        dataSource.setJdbcUrl(dbUrl2);
        dataSource.setUser(username2);
        dataSource.setPassword(password2);
        dataSource.setMaxPoolSize(20);
        dataSource.setMinPoolSize(5);
        dataSource.setInitialPoolSize(10);
        dataSource.setMaxIdleTime(300);
        dataSource.setAcquireIncrement(5);
        dataSource.setIdleConnectionTestPeriod(60);
        
        return dataSource;
    }
	
//	private DruidDataSource getDruidDataSource(String username, String password, String url) {
//        DruidDataSource datasource = new DruidDataSource();
// 
//        datasource.setUrl(url);
//        datasource.setUsername(username);
//        datasource.setPassword(password);
//        datasource.setDriverClassName(driverClassName);
// 
//        //configuration
//        datasource.setInitialSize(initialSize);
//        datasource.setMinIdle(minIdle);
//        datasource.setMaxActive(maxActive);
//        datasource.setMaxWait(maxWait);
//        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
//        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
//        datasource.setValidationQuery(validationQuery);
//        datasource.setTestWhileIdle(testWhileIdle);
//        datasource.setTestOnBorrow(testOnBorrow);
//        datasource.setTestOnReturn(testOnReturn);
//        datasource.setPoolPreparedStatements(poolPreparedStatements);
//        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
//        try {
//            datasource.setFilters(filters);
//        } catch (SQLException e) {
//            logger.error("druid configuration initialization filter : {0}", e);
//        }
//        datasource.setConnectionProperties(connectionProperties);
// 
//        return datasource;
//    }
	
}